package user.Controller.userInformationChange;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import user.utils.DBUitls;
import user.utils.JsonUntil;
import user.Model.entity.user;
import user.Model.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/*
* 获取修改完毕的用户信息
* 用session中存在的用户信息确定哪位用户，修改他的信息
* 返回修改完毕的数据
* */
@WebServlet(name = "userInformationServlet",urlPatterns = "/user/userInformationServlet")
public class userInformationServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转化成String类
        String jsonString=String.valueOf(JsonUntil.getJson(req));
        //转换成JSONObject类
        JSONObject jsonObject= JSON.parseObject(jsonString);
        //获取session中的用户名，确定要改变的数据是哪个用户
        req.getSession().setAttribute("nowUser",new user("王铭杰","123456","1502806050@qq.com","18734505818","王铭杰","Java","七期"));
        user nowUser = (user)req.getSession().getAttribute("nowUser");
        //获取前端传来的数据
        String username =  jsonObject.getString("username");
        String password =  jsonObject.getString("password");
        String postbox = jsonObject.getString("postbox");
        String phoneNumber = jsonObject.getString("phoneNumber");
        String name = jsonObject.getString("name");
        String direction = jsonObject.getString("direction");
        String phase = jsonObject.getString("phase");
        //向前端返回值
        //改变数据库中的用户信息
        int count = 0;
        try {
            //得到用户id确认
            count = Service.changeUserInformation(Service.getId(nowUser.getUserName()),username,password,postbox,phoneNumber,name,direction,phase);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                DBUitls.freeConnection();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        //创建新的session
        HttpSession session = req.getSession();
        session.removeAttribute("nowUser");
        session.setAttribute("nowUser",new user(username,password,postbox,phoneNumber,name,direction,phase));
        //向前端返回值
        PrintWriter out = resp.getWriter();
        JSONObject object = new JSONObject();
        if(count == 0){
            object.put("code" , "-1");
            object.put("message" , "用户信息修改失败");
            out.println(object);
        }
        else{
            object.put("username",username);
            object.put("password",password);
            object.put("postbox",postbox);
            object.put("phoneNumber",phoneNumber);
            object.put("name",name);
            object.put("direction",direction);
            object.put("phase",phase);
            object.put("code","200");
            object.put("message","用户信息修改成功");
            object.put("userType","user");
            out.println(object);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
